package io.github.luons.mediator.core.spi.cube;

import com.google.common.base.Preconditions;
import io.github.luons.mediator.core.enums.Aggregations;
import io.github.luons.mediator.core.validator.EnumMember;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * Column
 *
 * @author : luons
 */
@Data
public class Column extends BaseColumn {

    /**
     * scale
     */
    protected Integer scale = 2;

    /**
     * aggregation
     */
    @EnumMember(value = Aggregations.class, message = "aggregation is error")
    protected String aggregation;

    public Column() {
    }

    public Column(String column) {
        this(column, column);
    }

    public Column(String column, String alias, String aggregation) {
        Preconditions.checkNotNull(column, String.format("column %s must not be null", column));
        this.column = column;
        this.alias = alias;
        this.aggregation = aggregation;
    }

    public Column(String column, String alias) {
        Preconditions.checkNotNull(column, String.format("column %s must not be null", column));
        this.column = column;
        this.alias = alias;
        if (this.alias == null) {
            this.alias = column;
        }
    }

    public Column(String column, String alias, Aggregations aggregation) {
        Preconditions.checkNotNull(column, String.format("column %s must not be null", column));
        this.column = column;
        this.alias = alias;
        if (this.alias == null) {
            this.alias = column;
        }
        this.aggregation = aggregation.getName();
    }

    /**
     * alias is null return column
     *
     * @return String
     */
    public String getAliasDefault() {
        return StringUtils.isNotBlank(this.getAlias()) ? this.getAlias() : this.getColumn();
    }
}
